class Backend::NewsPagesController < ApplicationController
  layout 'admin_layout'

  def index
    @news = News.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @news }
    end
  end

  def show
    @news = News.find(params[:id])

    respond_to do |format|
      format.html
      format.xml  { render :xml => @news }
    end
  end

  def new
    @news = News.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @news }
    end
  end

  def edit
    @news = News.find(params[:id])
  end

  def create
    @news = News.new(params[:news])
    @news_upload = NewsUpload.new(params[:upload])
    @news_upload.news = (@news)

    respond_to do |format|
      if @news_upload.save
        if @news.save
          flash[:news_ss] = 'News was successfully created.'
          format.html { redirect_to(admin_news_page_path(@news.id)) }
          format.xml  { render :xml => @news, :status => :created, :location => @news }
        else
          flash[:news_err] = 'News was not successfully created.'
          format.html { render :action => "new" }
          format.xml  { render :xml => @news.errors, :status => :unprocessable_entity }
        end
      else
        flash[:news_err] = 'News was not successfully created.'
        format.html { render :action => "new" }
        format.xml  { render :xml => @news.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update
    @news = News.find(params[:id])
    @news_upload = NewsUpload.find_by_news_id(@news.id)
    if !@news_upload.nil?
      @news_upload.news = (@news)
    end
    respond_to do |format|
      if @news.update_attributes(params[:news])
        if !@news_upload.nil?
          if @news_upload.update_attributes(params[:upload])
            flash[:news_up_ss] = 'News was successfully updated.'
            format.html { redirect_to admin_news_page_path(@news.id) }
            format.xml  { head :ok }
          else
            flash[:news_up_ss] = 'News was not successfully updated.'
            format.html { render :action => "edit" }
            format.xml  { render :xml => @news.errors, :status => :unprocessable_entity }
          end
        else
          flash[:news_up_ss] = 'News was successfully updated.'
          format.html { redirect_to admin_news_page_path(@news.id) }
          format.xml  { head :ok }
        end
      else
        flash[:news_up_ss] = 'News was not successfully updated.'
        format.html { render :action => "edit" }
        format.xml  { render :xml => @news.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @news = News.find(params[:id])
    @news.destroy

    respond_to do |format|
      format.html { redirect_to(admin_news_pages_path) }
      format.xml  { head :ok }
    end
  end

  def remove
    @news_upload = NewsUpload.find_by_id(params[:with])
    @news = News.find_by_id(@news_upload.news_id)
    @news_upload.destroy

    redirect_to edit_admin_news_page_path(@news.id)
  end
end
